package cn.hx.controller.system;

import cn.hx.entity.Dept;
import cn.hx.service.DeptService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class DeptController {

    @Resource
    private DeptService deptService;


    @GetMapping("system/dept/deptList")
    public String deptLists(
            ModelMap modelMap,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size,
            Dept dept) {
        modelMap.addAttribute("deptPage",deptService.page(dept,page, size));
        return "system/dept/deptList";
    }

    @GetMapping("system/dept/deptEdit")
    public String add(ModelMap map,Dept dept){
        /*map.addAttribute("deptList",deptService.queryAllByLimit(dept,0,100));*/
        return "system/dept/deptEdit";
    }
    @Value("${web.upload-path}")
   private String filePath;
    @PostMapping("system/dept/deptEdit")
    public String addDept(Dept dept, MultipartFile file) throws IOException {
        if (file != null && !file.isEmpty()) {
            String filename = file.getOriginalFilename();
            String type = filename.substring(filename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            File file1 = new File(filePath + "/" + type);
            if (!file1.isDirectory()){
                file1.mkdirs();
            }
            file.transferTo(new File(filePath+"/" +type+"/"+ uuid + type));
            dept.setIcon("/"+type+"/" + uuid + type);
        }
        deptService.insert(dept);
        return "redirect:/system/dept/deptList";
    }

    @GetMapping("system/dept/deptEdit/{id}")
    public String update(@PathVariable Integer id,ModelMap map){
        map.addAttribute("dept",deptService.queryById(id));
        return "system/dept/deptEdit";
    }

    @PostMapping("system/dept/deptEdit/{id}")
    public String saveDept(Dept dept,MultipartFile file) throws IOException {
        if (file != null && !file.isEmpty()) {
            String filename = file.getOriginalFilename();
            String type = filename.substring(filename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//            file.transferTo(new File("C:\\Users\\Administrator\\IdeaProjects\\approval\\src\\main\\webapp\\img\\icons\\" + uuid + type));
//            dept.setIcon("/img/icons/" + uuid + type);
            File file1 = new File(filePath + "/" + type);
            if (!file1.isDirectory()){
                file1.mkdirs();
            }
            file.transferTo(new File(filePath+"/" +type+"/"+ uuid + type));
            dept.setIcon("/"+type+"/" + uuid + type);
        }
        deptService.update(dept);
        return "redirect:/system/dept/deptList";
    }

    @GetMapping("system/dept/deptList/{id}")
    public String delete(@PathVariable Integer id){
        deptService.deleteById(id);
        return "redirect:/system/dept/deptList";
    }
}
